/************************************************************/
/*															*/
/* $File      :  d3d_obj.h									*/
/* $Author    :  Sean Franklin								*/
/* $Info      :  X-Calibur image wrapper (with d3d)			*/
/*															*/
/************************************************************/



#ifndef _XCT_D3DWRAPPER_V01_H_
#define _XCT_D3DWRAPPER_V01_H_

#include <d3d9.h>
#include <d3dx9.h>
#include <windowsx.h>
#include <vector>
#include <fstream>
#include "PerformanceTimer.h"
#include "ObjectManager.h"

#define SCREEN_WIDTH 640 /* -- Enter screen width -- */
#define SCREEN_HEIGHT 480 /* -- Enter screen height -- */

/* COLOR SHORTCUTS */
#define BLACK D3DCOLOR_XRGB(0, 0, 0)
#define WHITE D3DCOLOR_XRGB(255, 255, 255)
#define DBLUE D3DCOLOR_XRGB(0, 0, 170)
#define DRED  D3DCOLOR_XRGB(170, 0, 0)
#define DYLLW D3DCOLOR_XRGB(215, 215, 0)
#define DGRN  D3DCOLOR_XRGB(0, 170, 0)
#define colorkey D3DCOLOR_ARGB

#define XEVIONSOFT_COLOR   colorkey(100, 0, 65, 255)
/* /////////////// */


typedef int IMAGEID;
typedef int FONTID;

/******************************************************/
/*													  */
/* Identifier     :  XCT_D3DOBJ						  */
/* Author         :  Sean Franklin					  */
/* Info           :  The main class of the wrapper							  
/*
/*                                                    */
class XCT_D3DOBJ
{
public:

	XCT_D3DOBJ(void){ d3dDevice = NULL; }
	~XCT_D3DOBJ(void){ /* Do nothing. */ }

	void InitXCT_D3D(HWND hWnd);
	
	__declspec(deprecated("This function may not be implemented in this wrapper.")) bool RenderScene(void){ return true; }
	void ClearColor(D3DCOLOR color);

	BOOL LoadSprite(char* FileName, D3DCOLOR ColorKey);


	bool RenderSprite(IMAGEID id, float x, float y, D3DCOLOR color);
	bool RenderFrame(IMAGEID id, float x, float y, RECT SrcRect, D3DCOLOR color);
	
	void ResetVP(void)
	{
		if ( !d3dDevice )
			return;

		D3DVIEWPORT9 vp;

		vp.Width = SCREEN_WIDTH;
		vp.Height = SCREEN_HEIGHT;
		vp.MinZ = 0.0f;
		vp.MaxZ = 1.0f;
		vp.X = 0;
		vp.Y = 0;

		d3dDevice->SetViewport(&vp);
	}


	void Begin(void);
	void End(void);
	
	void UnloadSprite(int id);
	void UnloadAllSprites(void);

	// XCT_D3DText
	BOOL LoadFont(int Height, UINT Weight, BOOL Italic, LPCTSTR pFontName);
	bool RenderText(FONTID id, LPCTSTR pText, int x, int y, D3DCOLOR color);


	void UnloadFont(FONTID id);
	void UnloadAllFonts(void);


	/////////////////////////////////////////////////////////
	//
	// Accessors
	char *FileName(IMAGEID id) const
	{
		return m_vImgs[id].szFileName;
	}
	UINT Width(int id){ return m_vImgs[id].ii.Width; }
	UINT Height(int id){ return m_vImgs[id].ii.Height; }
	//
	////////////////////////////////////////////////////////



private:

	void BeginScene(void){ d3dDevice->BeginScene(); }
	void BeginSprite(void){ d3dSpt->Begin(D3DXSPRITE_ALPHABLEND); }

	void EndScene(void){ d3dDevice->EndScene(); }
	void EndSprite(void){ d3dSpt->End(); }
	
	void Present(void){ d3dDevice->Present(NULL, NULL, NULL, NULL); }

private:

	LPDIRECT3D9 d3d;
	LPDIRECT3DDEVICE9 d3dDevice;
	LPD3DXSPRITE d3dSpt;
	
	struct XCTBitmap
	{
		LPDIRECT3DTEXTURE9 sprite;
		D3DXIMAGE_INFO ii;
		char *szFileName;
	};
	struct XCTFont
	{
		LPD3DXFONT font;
		LPCTSTR szFontName;
	};

	std::vector<XCTBitmap> m_vImgs;
	std::vector<XCTFont>   m_vFonts;
};


#endif /* _XCT_D3DWRAPPER_V01_H_ */